<template>
  <div>
    <div class="top-area">
      <el-input
        v-model="employeeName"
        placeholder="请输入员工姓名"
        clearable
        @keyup.native.enter="init"
        ><i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i
      ></el-input>
      <el-button
        @click="
          $router.push({
            name: 'EditEmployee',
            query: { isEdit: '0' },
          })
        "
        icon="el-icon-plus"
        >添加员工</el-button
      >
    </div>
    <div class="table-container">
      <el-table
        :border="true"
        :data="tableData"
        style="width: 100%"
        max-height="527"
        stripe
      >
        <el-table-column prop="name" label="员工姓名"> </el-table-column>
        <el-table-column prop="username" label="账号"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="status" label="账号状态">
          <template slot-scope="scope">
            {{ scope.row.status ? "正常" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'EditEmployee',
                  query: { isEdit: '1', id: scope.row.id },
                })
              "
              >编辑</el-link
            >
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              title="确认调整该账号的状态?"
              @onConfirm="handleChangeStatus(scope.row)"
              ><el-link type="danger" slot="reference">{{
                scope.row.status ? "禁用" : "启用"
              }}</el-link>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="el-pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.page"
          :page-sizes="pageSizes"
          :page-size="pageInfo.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getEmployees, changeEmployee } from "@/api/employee";
export default {
  data() {
    return {
      employeeName: "",
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      pageSizes: [10, 20, 30, 40],
      tableData: [],
    };
  },
  created() {
    this.init();
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.tableData.slice(start, start + this.pageSize);
    },
  },
  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.employeeName }).then(
        (res) => {
          const { records, total } = res.data;
          this.tableData = records;
          this.total = total;
        }
      );
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val; // 更新每页显示的数据条数
      this.pageInfo.page = 1; // 切换每页数据时重置到第一页
      this.init();
    },
    handleChangeStatus(row) {
      changeEmployee({ id: row.id, status: +!row.status }).then((res) => {
        this.$message.success("修改成功");
        this.init();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.top-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  margin-bottom: 15px;
  // background-color: red;

  // .el-autocomplete {
  .el-input {
    width: 310px;
    height: 60px;
    line-height: 60px;
    font-size: 16px;

    .el-input__inner {
      padding: 0 15px !important;
    }
  }

  .el-button {
    width: 140px;
    height: 40px;
    font-size: 16px;
    color: #333;
    background-color: #ffc200;
  }
}

.table-container {
  .el-table {
    border: solid 2px #f3f4f7;
    border-radius: 2px;
  }
  .el-pagination-container {
    display: flex;
    justify-content: center; /* 水平居中 */
    margin: 15px 0 0 0; /* 上下间距 */
  }
}
</style>
